/*
 * tcu_can.h
 *
 *  Created on: 2018-4-9
 *      Author: root
 */

#ifndef TCU_CAN_H_
#define TCU_CAN_H_

#include <stdio.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/error.h>
#include <linux/can/raw.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "share_type.h"
#include "protocol_can.h"
#include "with_ui.h"
#ifdef __cplusplus
extern "C"{
#endif
//#define CANNUM   2

#define TCU_CAN_ADDR	0x8a

#define CCU_CAN_BMS_ADDR		0xF4
#define CCU_CAN_CHARGER_ADDR	0x56

#define errout(_s)	fprintf(stderr, "error class: %s\n", (_s))
#define errcode(_d) fprintf(stderr, "error code: %02x\n", (_d))
#define myerr(str)	fprintf(stderr, "%s, %s, %d: %s\n", __FILE__, __func__, __LINE__, str)
#define  HSSY       1     //华商三优
#define  XJ         2     //许继
#define SZHS        3     //苏州和顺
#define SXDZ        4     //三星电子
#define GZKL        5     //广州科力

typedef struct
{
	unsigned char p1[4];   //厂家编码
	unsigned char p2[2];   //充电桩设备型号
	unsigned char p3;	   //充电桩所支持的 BMS 通信协议版本号接收到主站发来的充电要求,控制器处于准备好状态
	unsigned char p4[12];  //充电控制器序列号
	unsigned char p5[2];   //充电控制器硬件版本
	unsigned char p6[3];   //充电控制器软件版本
	unsigned char p7[4];   //充电控制器软件日期
	short p8;	//充电机最高输出电压
	short p9;	//充电机最低输出电压
	short p10;  //充电机最大输出电流
	short p11;  //充电机最小输出电流
} gun_con;

typedef struct
{
	int MX1;	// 负荷控制开关
	int MX2;	// 成功标识
	int MX3;	// 失败原因

	struct
	{
		unsigned char  MX4[3];	//充电桩与BMS通信协议版本号
		unsigned char  MX5[3];	//BMS与充电桩通信协议版本号
		unsigned char  MX6;	//充电桩与 BMS握手结果
		unsigned char  MX7;	//电池类型
		unsigned char  MX8;	//最高允许温度
		unsigned short MX9; //BMS 最高允许充电电压
		unsigned short MX10;//单体最高允许充电电压
		unsigned short MX11;//最高允许充电电流
		unsigned short MX12;//整车动力蓄电池额定总电压
		unsigned short MX13;//整车动力蓄电池当前电压
		unsigned short MX14;//整车动力蓄电池额定容量
		unsigned short MX15;//整车动力蓄电池标称能量
		unsigned short MX16;//整车动力蓄电池荷电状态
		unsigned short MX17;//充电机最高输出电压
		unsigned short MX18;//充电机最低输出电压
		unsigned short MX19;//充电机最大输出电流
		unsigned short MX20;//充电机最小输出电流
		unsigned char  MX21[17];//车辆识别码
	}dc;
}start_mx;

typedef struct
{
	unsigned char MX1;	//   停止原因
	unsigned char MX2;	// 成功标识
	struct
	{

		unsigned char MX3;	// BMS 中止充电原因

		unsigned char MX4[2];	//BMS 充电故障原因

		unsigned char MX5;	//BMS 中止错误原因

		unsigned char MX6;	//中止荷电状态SOC（%）

		short MX7;	//动力蓄电池单体最低电压
		short MX8;	// 动力蓄电池单体最高电压
		unsigned char MX9;//动力蓄电池最低温度
		unsigned char MX10;// 动力蓄电池最高温度
		unsigned short MX11;//接收SPN2560=0x00的充电机辨识报文超时
		unsigned short MX12;//接收SPN2560=0xAA的充电机辨识报文超时
		unsigned short MX13;//接收充电机的时间同步和充电机最大输出能力报文超时
		unsigned short MX14;//接收充电机完成充电准备报文超时
		unsigned short MX15;//接收充电机充电状态报文超时
		unsigned short MX16;//接收充电机中止充电报文超
		unsigned short MX17;//接收充电机充电统计报文超时
		unsigned char MX18[6];// BMS 其他错误
		unsigned short MX19;//接收 BMS 和车辆的辨识报文超时
		unsigned short MX20;//接收电池充电参数报文超时
		unsigned short MX21;	// 接收 BMS 完成充 电准备报文超时
		unsigned short MX22;//接收电池充电总状 态报文超时
		unsigned short MX23;//接收电池充电要求报文超时
		unsigned short MX24;//接收 BMS 中止充电报文超时
		unsigned short MX25;//接收 BMS 充电统计报文超时
		unsigned char MX26[6];	// 充电机其他错误

	}dc;
}end_mx;
typedef struct
 {
	  long msgtype;
	  char pmsg[sizeof(GW_Can)+4];
}msgtcu;

typedef struct can_frame CanMsg;
extern canlink* g_pcan[];
extern Gun_Can_Info gun_can_info_tab[];
extern start_mx g_startInfo[];
extern can_data g_candata;
//extern int g_qmegid[];
extern u8 gm_sys_state[];
void handle_err_frame(CanMsg *fr);
int init_can(int chan, int samplate);
int initcansocket(u8 port,int* pS);
int FinishStopCharge(u8 gunid,void* pdata);
u8 getChargeState(u8 gunid);//获得枪的充电状态
u8 get_gun_conn_state(u8 gunid);//获得充电枪连接状态
u8 get_gun_err(u8 gunid,u8 flag_save);//获得充电枪错误代码  0 无错误
u8 getCpState(u8 gunid);
u8 writeErrToDb(u8 gunid,u8 type);
void print_frame(CanMsg *fr);
u8 get_gun_err_new(u8 gunid,u8* errcodeflag);

int bak_ctrl_pw(u8 gunid,void* pdata);
#ifdef __cplusplus

}

#endif
#endif /* TCU_CAN_H_ */
